<template>
  <div class="app-container">
    <el-form ref="queryRef" :model="queryParams" :inline="true">
      <el-form-item label="姓名" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="全部"
          clearable
          style="width: 150px"
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="报名时间" prop="enrollTime">
        <el-date-picker
          v-model="enrollTimeArr"
          type="datetimerange"
          align="right"
          value-format="YYYY-MM-DD HH:mm:ss"
          unlink-panels
          range-separator="至"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="20">
        <el-button type="primary" plain @click="verityMore" :disabled="multiple"
          >批量审核</el-button
        >
        <el-button type="primary" plain @click="goAddPerson(trainId)"
          >新增人员</el-button
        >
        <el-button type="warning" plain @click="handleExport"
          >导出报名人员</el-button
        >
      </el-col>
    </el-row>

    <!-- 表格数据 -->
    <el-table
      v-loading="loading"
      :data="tableData"
      @selection-change="handleSelectionChange"
    >
      <el-table-column
        type="selection"
        width="55"
        align="center"
        fixed="left"
      />
      <el-table-column label="姓名" prop="name" min-width="100" />
      <el-table-column label="性别" prop="sex" min-width="50" />
      <el-table-column label="报名时间" prop="enrollTime" min-width="150" />
      <el-table-column label="培训项目" prop="trainProject" min-width="200" />
      <el-table-column label="考试项目" prop="examProjects" min-width="200" />
      <el-table-column label="选择酒店" prop="hotels" min-width="150" />
      <el-table-column label="费用合计" prop="totalCost" min-width="100" />
      <el-table-column label="缴纳费用" prop="payment" min-width="100" />
      <el-table-column label="支付方式" prop="payTypeStr" min-width="100" />
      <el-table-column label="报名状态" prop="auditStatusStr" min-width="80" />
      <el-table-column
        label="操作"
        align="center"
        width="220"
        fixed="right"
        class-name="small-padding"
      >
        <template #default="scope">
          <el-button
            v-if="scope.row.auditStatus == 1 && scope.row.payStatus == 3"
            type="text"
            @click="verityPerson(scope.row, 1)"
            >审核通过</el-button
          >

          <el-button
            v-if="scope.row.auditStatus == 1 && scope.row.payStatus == 3"
            type="text"
            @click="verityPerson(scope.row, 2)"
            >审核拒绝</el-button
          >
          <el-button
            v-if="scope.row.auditStatus == 2"
            type="text"
            @click="verityPerson(scope.row, 3)"
            >取消</el-button
          >
          <el-button
            v-if="scope.row.auditStatus == 1 && scope.row.payStatus == 1"
            type="text"
            @click="refund(scope.row, 2)"
            >核销</el-button
          >
          <el-button type="text" @click="particulars(scope.row)"
            >详情</el-button
          >
          <!-- <el-button
            v-if="
              scope.row.status == 2 &&
              scope.row.payStatus == 6 &&
              scope.row.payType == 1
            "
            type="text"
            @click="refund(scope.row, 0)"
            >审核拒绝线下付款核销</el-button
          > -->
          <el-button
            v-if="
              (scope.row.auditStatus == 4 || scope.row.auditStatus == 3) &&
              scope.row.payStatus == 6 &&
              scope.row.payType == 1
            "
            type="text"
            @click="refund(scope.row, 3)"
            >退款核销</el-button
          >
          <!-- <el-text v-if="scope.row.status == 2 && scope.row.payStatus == 5"
            >审核拒绝</el-text
          >
          <el-text v-if="scope.row.status == 4 && scope.row.payStatus == 5"
            >订单已取消</el-text
          > -->
        </template>
      </el-table-column>
    </el-table>
    <pagination
      v-show="total > 0"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      :total="total"
      @pagination="getList"
    />
  </div>
</template>

<script setup name="personVerity">
import { getCurrentInstance, ref } from "vue";
import { onMounted, reactive } from "@vue/runtime-core";
import { toRefs } from "@vueuse/shared";

import {
  getVerityList,
  verity,
  batchVerity,
  refundHexiao,
} from "../../../../api/train/verity";
import { useRouter } from "vue-router";
import { useRoute } from "vue-router";

const router = useRouter();
const route = useRoute();
const { proxy } = getCurrentInstance();

const showSearch = ref(true);
const enrollTimeArr = ref([]);
const tableData = ref([]);
const total = ref(0);
const loading = ref(true);
const trainId = route.params.trainId;
// let flag = undefined
let selectedIds = [];
const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    name: undefined,
    trainId: trainId,
  },
  rules: {},
  multiple: true,
});
const { queryParams, form, rules, multiple } = toRefs(data);

//获取列表
function getList() {
  loading.value = true;
  if (enrollTimeArr.value) {
    queryParams.value.enrollTimeRange = enrollTimeArr.value.toString();
  }
  getVerityList(queryParams.value).then((res) => {
    loading.value = false;
    tableData.value = res.rows;
    total.value = res.total;
  });
}

function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

function resetQuery() {
  proxy.resetForm("queryRef");
  enrollTimeArr.value = [];
  trainId;
  handleQuery();
}

// 新增人员
function goAddPerson(trainId) {
  router.push({ name: "addPerson", params: { trainId: trainId } });
}

//审核人员
function verityPerson(row, flag) {
  const param = { id: row.orderId, flag: flag };
  if (1 == flag) {
    proxy.$modal
      .confirm("确定审核通过吗？")
      .then(() => {
        return verity(param);
      })
      .then(() => {
        getList();
        proxy.$modal.msgSuccess("审核成功！");
      });
  } else if (2 == flag) {
    proxy.$modal
      .confirm("确定审核拒绝吗？")
      .then(() => {
        return verity(param);
      })
      .then(() => {
        getList();
        proxy.$modal.msgSuccess("报名已退回！");
      });
  } else if (3 == flag) {
    proxy.$modal
      .confirm("确定订单取消吗？")
      .then(() => {
        return verity(param);
      })
      .then(() => {
        getList();
        proxy.$modal.msgSuccess("订单已取消！");
      });
  } else if (4 == flag) {
    proxy.$modal
      .confirm("确定通过核销？")
      .then(() => {
        return verity(param);
      })
      .then((data) => {
        getList();
        if (data.data.failure == undefined) {
          proxy.$modal.msgSuccess(data.data.success);
        } else {
          proxy.$modal.msgError(data.data.failure);
        }
      });
  }
}

//线下支付退款审核
function refund(row, flag) {
  const param = { id: row.orderId, flag: flag };
  if (0 == flag) {
    proxy.$modal
      .confirm("确定审核拒绝线下退款核销吗？")
      .then(() => {
        return refundHexiao(param);
      })
      .then(() => {
        getList();
        proxy.$modal.msgSuccess("审核拒绝线下退款核销成功！");
      });
  } else if (3 == flag) {
    proxy.$modal
      .confirm("确定订单取消线下退款核销吗？")
      .then(() => {
        return refundHexiao(param);
      })
      .then(() => {
        getList();
        proxy.$modal.msgSuccess("订单取消线下退款核销成功！");
      });
  } else if (2 == flag) {
    proxy.$modal
      .confirm("确定核销该订单吗？")
      .then(() => {
        return refundHexiao(param);
      })
      .then(() => {
        getList();
        proxy.$modal.msgSuccess("订单核销成功！");
      });
  }
}

//批量审核人员
function verityMore() {
  let param = { ids: selectedIds };
  var title = `是否确认审批 ${selectedIds.length} 个报名人员？`;
  proxy.$modal
    .confirm(title)
    .then(function () {
      return batchVerity(param);
    })
    .then(() => {
      getList();
      proxy.$modal.msgSuccess("审批成功！");
    });
}

//导出报名人员
function handleExport() {
  proxy.download(
    "/systemj/signer/exportEnrollsVerify",
    {
      ...queryParams.value,
    },
    `报名人员_${new Date().getTime()}.xlsx`
  );
}

function handleSelectionChange(selection) {
  selectedIds = selection.map((item) => item.id);
  multiple.value = !selection.length;
}

// 前往详情页
function particulars(row) {
  router.push({
    path: "/train/particulars",
    query: {
      id: row.orderId,
      payType: row.payType,
      status: row.auditStatus,
    },
  });
}
// sortable
onMounted(() => {
  getList();
});
</script>

<style scoped></style>
